<?php
defined('IN_PHPCMS') or exit('No permission resources.');
// 模型缓存路径
define('CACHE_MODEL_PATH', CACHE_PATH . 'caches_model' . DIRECTORY_SEPARATOR . 'caches_data' . DIRECTORY_SEPARATOR);
// 定义在单独操作内容的时候，同时更新相关栏目页面
define('RELATION_HTML', true);

pc_base::load_app_class('admin', 'admin', 0);
pc_base::load_sys_class('form', '', 0);
pc_base::load_app_func('util');
pc_base::load_sys_class('format', '', 0);

/**
 * 游戏绑定人才
 */
class talent extends admin
{

    private $db,$tsmodel,$talent_pic_model;

    public $siteid;

    public function __construct()
    {
        parent::__construct();
        $this->db = pc_base::load_model('news_nsr_model');
        $this->tsmodel = pc_base::load_model('pass_talent_model');
        $this->talent_pic_model = pc_base::load_model('pass_talent_pic_model');
        $pass_game_model=pc_base::load_model('pass_game_model');
        $this->siteid = get_siteid();
        $model_setting=pc_base::load_model('site_setting_model');
        $this->t_type = $model_setting->getValue("talent_type",$this->siteid);//获取人才类型
    }

    /**
     * 关卡删除、批量删除绑定人才
     */
    public function delete()
    {
        if ($_GET['talent']){
            $this->tsmodel = pc_base::load_model('pass_talent_model');
        }
        if (! isset($_REQUEST['id']) || empty($_REQUEST['id'])) {
            showmessage(L('illegal_action'), HTTP_REFERER);
        }
        $id = $_REQUEST['id'];
        $real_name = $_REQUEST['real_name']?$_REQUEST['real_name']:"请联系管理员";
        $pass_id = $_REQUEST['pass_id'];
        $pass_name = $_REQUEST['name']?$_REQUEST['name']:"请联系管理员";
        $cid = '';
        $talent_id = '';
        if (is_array($id)) {
            foreach ($id as $key=>$value){
                $cid .= ','.$value;
                //查询出该关卡所绑定的人才id
                $tids_result = $this->tsmodel->select(array('id'=>$value),"tid");
                $talent_id .= ','.$tids_result[0]['tid'];
            }
            $cid = substr($cid, 1);
            $talent_id = substr($talent_id, 1);
        }else{
            //查询出该关卡所绑定的人才id
            $tids_result = $this->tsmodel->select(array('id'=>$id),"tid");
            $talent_id .= $tids_result[0]['tid'];
            $cid = $id;
        }
        //删除游戏绑定人才、人才绑定的图片
        $result = $this->tsmodel->delete("id in (".$cid.")");
        $result_del_pic = $this->talent_pic_model->delete(" tid in (".$talent_id.")");

        //记录行为日志
        $log_content = '<span style="color:red;">删除游戏绑定的人才</span>[人才id：'.$talent_id.'；人才姓名：'.$real_name.']';
        $this->manage_admin_log($log_content);

        if ($result) {
            showmessage(L('operation_success'), HTTP_REFERER);
        } else {
            showmessage(L('operation_failure'), HTTP_REFERER);
        }
    }

    /**
     * 人才选手删除、批量删除绑定的照片
     */
    public function delete_pic()
    {
        if (! isset($_REQUEST['id']) || empty($_REQUEST['id'])) {
            showmessage(L('illegal_action'), HTTP_REFERER);
        }
        $id = $_REQUEST['id'];
        $real_name = $_REQUEST['real_name'];#人才真实姓名
        $talent_id = $_REQUEST['talent_id'];
        if (is_array($id)) {
            $id = implode('\',\'', $id);
        }
        $result = $this->talent_pic_model->delete("id in ('$id')");

        if ($result) {
            //记录行为日志
            $log_content = '<span style="color:red;">游戏选手管理：删除人才所绑定的图片</span>[人才id：'.$talent_id.'；人才姓名：'.$real_name.']';
            $this->manage_admin_log($log_content);
            showmessage(L('operation_success'), HTTP_REFERER);
        } else {
            showmessage(L('operation_failure'), HTTP_REFERER);
        }
    }

    /**
     * 排序
     */
    public function listorder() {
        if ($_GET['talent']){
            $this->tsmodel = pc_base::load_model('pass_talent_model');
        }
        if(isset($_GET['dosubmit'])) {
            foreach($_POST['listorders'] as $id => $listorder) {
                $res = $this->tsmodel->update(array('list_order'=>$listorder), array('id'=>$id));
            }
            //记录行为日志
            $log_content = '<span style="color:orange;">关卡管理：人才排序</span>';
            $this->manage_admin_log($log_content);
            showmessage(L('operation_success'));
        } else {
            showmessage(L('operation_failure'));
        }
    }

    /**
     * 关卡游戏-人才-关联图集中的组图
     */
    public function pic_manage() {
        $talent_id = intval($_GET['talent_id']);#人才id
        //非法请求
        if (! isset($talent_id) || empty($talent_id)) {
            showmessage(L('illegal_action'), HTTP_REFERER);
        }
        $where = '';#初始查询条件为空
        $pass_id = intval($_GET['pass_id']);#关卡id
        $name = $_GET['name'];#关卡名字
        $real_name = $_GET['real_name'];#选手真实姓名
        
        $pass_talentpic_model = pc_base::load_model('pass_talent_pic_model');#加载关卡中的人才图片关联model
        $talent_source_model = pc_base::load_model('talent_source_model');#加载人才资源关联model
        $picture_model = pc_base::load_model('picture_nsr_model');#加载图集model
        //查询该人才所绑定的图集
        $pics_ids = $talent_source_model->select(array('talent_id'=>$talent_id,'source_type'=>3),'source_id');
        //查询所有图集中所有的组图
        $tmp_pics_ids_arr = array();
        foreach ($pics_ids as $key=>$value){
            $tmp_pics_ids_arr[] = $value['source_id'];
        }
        $pic_id = implode('\',\'', $tmp_pics_ids_arr);#所有的图集id
        $pictureurls = $picture_model->select(' id in('."'".$pic_id."'".')','pictureurls');#所有的组图-array
        $picurls_all = array();#所有的单张图片
        foreach ($pictureurls as $k=>$v){
            $test = json_decode($v['pictureurls']);
            foreach ($test as $v2=>$k2) {
                $picurls_all[] = $k2->url;
            }
        }
        unset($tmp_pics_ids_arr);
        unset($pic_id);
        unset($pictureurls);
        //查询关卡模块:人才与图片的关联数据
        $pics_ids = $pass_talentpic_model->select(array('tid'=>$talent_id),'id');
        $where = ' tid='.$talent_id;
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;
        //获取管理员信息
        $admin_model = pc_base::load_model('admin_model'); // 加载admin_model
        $administrators = $admin_model->getRealnameInfo();
        $pics_ids=$pass_talentpic_model->listinfo($where,'id DESC',$page,10);
        $pages = $pass_talentpic_model->pages;
        include $this->admin_tpl('talent_pic_list');
    }

    /**
     * 关卡游戏-人才-单独添加选手图片
     */
    public function add_talent_pic() {
        $talent_id = intval($_REQUEST['talent_id']);#人才id
        $real_name = $_REQUEST['real_name'];#人才姓名
        //非法请求
        if (! isset($talent_id) || empty($talent_id)) {
            showmessage(L('illegal_action'), HTTP_REFERER);
        }
        //后台编辑进行图片上传操作
        if(!empty($_POST['data']['pic_url'])){
            $post_data = $_POST['data'];
            $data['tid']=$post_data['talent_id'];
            $data['pic_url'] =  $post_data['pic_url'];
            $data['created_by'] =  $_SESSION['userid'];
            $data['create_time'] = date('Y-m-d H:i:s');
            $data['siteid'] = $this->siteid;
            
            $result = $this->talent_pic_model->insert($data,true);
            if ($result) {
                $id = $this->db->insert_id();
            	//记录行为日志
            	$log_content = '<span style="color:green;">关卡-选手管理-添加选手图片</span>[选手id：'.$talent_id.'；选手姓名：'.$real_name.'；图片URL：'.$data['pic_url'].']';
            	$this->manage_admin_log($log_content);
                $url='?m=pass_game&c=talent&a=pic_manage&talent_id='.$talent_id.'&name='.$real_name;
                showmessage(L('add_success'), $url);
            } else {
                showmessage(L('add_error'), HTTP_REFERER);
            }
        }else{
            echo "<script>alert('您尚未选择图片，请稍后重试！');history.go(-1);</script>";
        }
    }

    /**
     * 关卡游戏-人才-修改人才描述、纵图
     */
    public function pic_manage_edit() {
        //如果没获取到 talent_id，则视为非法请求
        $talent_id = intval($_REQUEST['talent_id']);#人才id
        //非法请求
        if (! isset($talent_id) || empty($talent_id)) {
            showmessage(L('illegal_action'), HTTP_REFERER);
        }
        $upinfo = $_POST['data'];
        $pass_id = intval($_REQUEST['pass_id'])?$pass_id:0;#关卡id
        $name = $_GET['name'];#关卡名字
        $real_name = $_GET['real_name'];#选手真实姓名

        $pass_talent_model=pc_base::load_model('pass_talent_model');
        
        //提交表单
        if (isset($_POST['dosubmit'])) {
            $data = isset($_POST['data']) ? $_POST['data'] : showmessage(L('illegal_parameters'), HTTP_REFERER);
            $data['updated_by'] =  $_SESSION['userid'];
            $data['update_time'] = date("Y-m-d H:i:s",time());
            $data['siteid'] = $this->siteid;
            $data['desc']   = $_POST['data']['desc'];
            $data['image_vertical'] = $_POST['data']['image_vertical'];
            $result = $pass_talent_model->update($data, array(
            'tid' => $talent_id
            ));
            if ($result) {
                //记录行为日志
                $log_content = '<span style="color:blue;">修改人才信息</span>[人才id：'.$talent_id.'；人才名称：'.$real_name.']';
                $this->manage_admin_log($log_content);
                $url='?m=pass_game&c=manage&a=pass_talent&pass_id='.$pass_id;
                showmessage(L('edit_success'), $url);
            } else {
                showmessage(L('edit_error'), HTTP_REFERER);
            }
        } else {
            $talent_info = $pass_talent_model->select(array('tid'=>$talent_id),'desc,image_vertical');
            $data = $talent_info[0];
            $show_header = '';
            include $this->admin_tpl('talent_info_edit');
        }
    }
    

    /**
     * 选择人才绑定
     */
    public function bindTalent(){
        $types = $this->t_type;
        $id= $_REQUEST['id'];
        $pass_talent_model=pc_base::load_model('pass_talent_model');
        $talent_array = $pass_talent_model->select('',"tid");//查询已关联的人才id
        $where = " status = 1 AND siteid = " . $this->siteid;
        $ids = array();
        if(!empty($talent_array)){
            foreach ($talent_array as $k => $v) {
                $ids[] = intval($v['tid']?$v['tid']:$v['tid']);
            }
            $ids = implode('\',\'', $ids);
            $where.=" and id not in ('$ids')";
        }
        if (isset($_GET['real_name']) && !empty($_GET['real_name'])) {
            $name = trim($_GET['real_name']);
            $where .= " and `real_name` like '%$name%' ";
        }
        if (isset($_GET['talent_type']) && !empty($_GET['talent_type'])) {
            switch ($_GET['talent_type']) {
                case '1':
                    $where .= " and is_student = 1";
                    break;
                case '2':
                    $where .= " and is_signed_model = 1";
                    break;
                case '3':
                    $where .= " and is_hot = 1";
                    break;
                case '4':
                    $where .= " and is_judge = 1";
                    break;
                case '5':
                    $where .= " and is_guest = 1";
                    break;
                case '6':
                    $where .= " and is_star = 1";
                    break;
                case '7':
                    $where .= " and is_mentor = 1";
                    break;
                case '8':
                    $where .= " and is_moter = 1";
                    break;
                default:
                    break;
            }
        }
        $talent_model = pc_base::load_model('talent_model');
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        $infos=$talent_model->listinfo($where,'id DESC',$page,10);
        $pages = $talent_model->pages;
        include $this->admin_tpl('bindTalent');
    }
    

    /**
     * 关卡：人才绑定图集中的图片
     */
    public function bindTalentPic(){
        $talent_id = intval($_GET['talent_id']);#人才id
        //非法请求
        if (! isset($talent_id) || empty($talent_id)) {
            showmessage(L('illegal_action'), HTTP_REFERER);
        }
        $real_name = $_REQUEST['real_name'];
        $pass_talentpic_model = pc_base::load_model('pass_talent_pic_model');#加载关卡中的人才图片关联model
        $talent_source_model = pc_base::load_model('talent_source_model');#加载人才资源关联model
        $picture_model = pc_base::load_model('picture_nsr_model');#加载图集model
        //查询该人才已经绑定的照片
        $already_bind_pics = $pass_talentpic_model->select(array('tid'=>$talent_id),'pic_url');
        $already_bind_pics_tmp= array();
        foreach($already_bind_pics as $key1=>$value2){
            $already_bind_pics_tmp[] = $value2['pic_url'];
        }
        //查询该人才所绑定的图集
        $pics_ids = $talent_source_model->select(array('talent_id'=>$talent_id,'source_type'=>3),'source_id');
        //查询所有图集中所有的组图
        $tmp_pics_ids_arr = array();
        foreach ($pics_ids as $key=>$value){
            $tmp_pics_ids_arr[] = $value['source_id'];
        }
        $pic_id = implode('\',\'', $tmp_pics_ids_arr);#所有的图集id
        $pictureurls = $picture_model->select(' id in('."'".$pic_id."'".')','pictureurls');#所有的组图-array
        $picurls_all = array();#所有的单张图片
        foreach ($pictureurls as $k=>$v){
            $test = json_decode($v['pictureurls']);
            foreach ($test as $v2=>$k2) {
                //去除已经绑定过的照片
                if(!in_array($k2->url, $already_bind_pics_tmp)){
                    $picurls_all[] = $k2->url;
                }
            }
        }
        include $this->admin_tpl('bindTalentPic');
    }

    /**
     * 关卡-人才：完成绑定
     */
    public function bindContestAjax(){
        $talent_model=pc_base::load_model('talent_model');
        $id = $_REQUEST['id'];
        $pass_id  = $_REQUEST['pass_id'];
        $pass_name = $_REQUEST['pass_name'];
        $relation=$_REQUEST['relation'];
        $contest=explode("|",$relation);
        $data=array();
        $user_name = '';#人才姓名
        $user_id = '';#人才id
        foreach($contest as $k=>$v){
            //循环查询选手：真实姓名、身高、体重、个人简介、省份[对应小游戏的赛区概念]插入到关卡人才关联表
            $talent_info = $talent_model->select(array("id"=>$v),"real_name,introduction,image_vertical");
            $data[$k]['desc']=strip_tags($talent_info[0]['introduction']);
            $data[$k]['image_vertical']=$talent_info[0]['image_vertical'];//取人才库中的纵图进行展示
            $data[$k]['pid']=$pass_id;
            $data[$k]['tid']=$v;
            //$data[$k]['list_order'] =  0;
            $data[$k]['created_by'] =  $_SESSION['userid'];
            $data[$k]['create_time'] = date('Y-m-d H:i:s');
            $user_name .= ','.$talent_info[0]['real_name'];
            $user_id .= ','.$v;
        }
        $res = $this->tsmodel->multiInsert($data);

        if($res){
            $user_id = substr($user_id, 1);
            $user_name = substr($user_name, 1);
            //记录行为日志
            $log_content = '<span style="color:green;">游戏绑定人才</span>[人才id：'.$user_id.'；人才姓名：'.$user_name.']';
            $this->manage_admin_log($log_content);
            echo  json_encode(array("status"=>true,"code"=>200,"msg"=>"选择成功"));
        }else{
            echo  json_encode(array("status"=>false,"code"=>1000,"msg"=>"选择失败"));
        }
    }

    /**
     * 人才-图片：完成绑定
     */
    public function bindPicAjax(){
        $id = $_REQUEST['id'];
        $talent_id  = $_REQUEST['talent_id'];
        $real_name = $_REQUEST["real_name"];
        $relation=$_REQUEST['relation'];
        $contest=explode("|",$relation);
        $data=array();
        foreach($contest as $k=>$v){
            $data[$k]['tid']=$talent_id;
            $data[$k]['pic_url'] =  $v;
            $data[$k]['created_by'] =  $_SESSION['userid'];
            $data[$k]['create_time'] = date('Y-m-d H:i:s');
            $data[$k]['siteid'] = $this->siteid;
        }
        $res = $this->talent_pic_model->multiInsert($data);
        
        //记录行为日志
        $log_content = '<span style="color:green;">游戏选手管理:人才绑定图片</span>[人才id：'.$talent_id.'；人才姓名：'.$real_name.']';
        $this->manage_admin_log($log_content);

        if($res){
            echo  json_encode(array("status"=>true,"code"=>200,"msg"=>"选择成功"));
        }else{
            echo  json_encode(array("status"=>false,"code"=>1000,"msg"=>"选择失败"));
        }
    }
}